package hotLeetcode;

import org.junit.internal.builders.NullBuilder;

public class H543Leetcode {
    public class TreeNode {
        int val;
        TreeNode left;
        TreeNode right;

        TreeNode() {
        }

        TreeNode(int val) {
            this.val = val;
        }

        TreeNode(int val, TreeNode left, TreeNode right) {
            this.val = val;
            this.left = left;
            this.right = right;
        }
    }

    int ans = 0;
    public int diameterOfBinaryTree(TreeNode root) {
        ans = 1;
        deep(root);
        return ans - 1;

    }

    private int  deep( TreeNode root) {
        if (root == null) return 0;


        int left = deep(root.left);
        int right = deep(root.right);
        ans = Math.max(ans, left + right + 1);

        return Math.max(left, right) + 1;


    }



}
